<?php
namespace app\api\controller\xilumarket;

use app\common\controller\Api;
use app\common\model\xilumarket\TopicUser;
use app\common\model\xilumarket\TopicAd as XilumarketTopicAd;

use function fast\array_get;

class TopicAd extends Api{
    
    protected $noNeedLogin = ['getList','getTimeRange', 'detail'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        
        $topicId = input('topic_id');
        if(!$topicId) {
            $this->error('参数错误');
        }
        $topicUser = TopicUser::where(['topic_id' => $topicId, 'user_id' => $this->auth->id])->find();
        if(!$topicUser || !$topicUser->checkAdmin()){
            $this->error('您没有权限');
        }
    }

    public function getAllList() {
        $topicId = input('topic_id');
        $list = XilumarketTopicAd::where(['topic_id' => $topicId])->paginate(input('pagesize', 10));
        $this->success('获取成功', $list);
    }

    public function getList() {
        $topicId = input('topic_id');
        if(!$topicId) {
            $this->error('参数错误');
        }
        $time = date('H:i');
        $timeRange = XilumarketTopicAd::getTimerangeArray();
        $code = '';
        foreach($timeRange as $code => $tr){
            if($tr[0]<= $time && $time <= $tr[1]){
                break;
            }
        }
        $list = XilumarketTopicAd::where(['timerange' => $code, 'topic_id' => $topicId])->select();
        $this->success('获取成功', $list);
    }

    public function detail($id) {
        $ad = XilumarketTopicAd::find($id);
        $this->success('成功',$ad);
    }

    public function add() {
        $topicId = input('topic_id');
        $title = input('title');
        $images = [input('image')];
        $url = input('url');
        
        $code = input('code', '');

        if(empty($topicId) || empty($title) || empty($images) || empty($url)){
            $this->error('参数错误');
        }
        $ad = XilumarketTopicAd::create([
            'topic_id' => $topicId,
            'title' => $title,
            'images' => $images,
            'url' => $url,
            'timerange' => $code,
            'create_by' => $this->auth->id,
            'create_time' => time(),
            'update_time' => time(),
        ]);
        $this->success('添加成功', $ad);
    }

    /**
     * 修改广告
     *
     * @param int $id
     * @return void
     */
    public function update($id) {
        $tad = XilumarketTopicAd::where(['topic_id'=>input('topic_id')])->find($id);
        if (!$tad) {
            $this->error('广告不存在');
        }
        $params = $this->request->post();
        $params['images'] = [array_get($params, 'image')];
        $params['timerange'] = array_get($params, 'code');
        $tad->save(array_filter($params, function($v, $k) {
            return in_array($k, ['title', 'images', 'timerange', 'url']);
        }, ARRAY_FILTER_USE_BOTH));
        $this->success('修改成功', $tad);
    }

    public function delete($id){
        $res = XilumarketTopicAd::destroy($id);
        
        $this->success('修改成功', $res);
    }
}